<?php

namespace app\index\controller;
use think\Controller;
use think\View;
use think\cache\driver\Redis;
class RedisProduct extends Base
{
    public function index(){
        $redis = new Redis();
        $redis->connect('127.0.0.1',6379);

        $redis->watch('sales');

        $sales = $redis->get('sales');
        $store = 2;
        if($sales>=$store){
            exit('活动结束');
        }
        //事务处理
        $redis->multi();
        $redis->set('sales',$sales+1);//incre();
        $res = $redis->exec();
        /**
         * 守护进程 swoole
         */
        if($res){
            $cc = Db::table('product')->where(['id'=>1])->update('store');
            if($cc){
                exit('抢购成功');
            }
        }
    }
}